package com.john.booter.domain.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang3.builder.ToStringBuilder;

import com.fasterxml.jackson.annotation.JsonFormat;

@Entity
@Table(name = "t_file")
public class File implements Serializable {
	
	private static final long serialVersionUID = 8044419212329008032L;
	
	/**
	 * id
	 */
	@Id
	@Column(columnDefinition = "CHAR(48)")
	private String id;
	/**
	 * 类型e.g. jpg,png......
	 */
	@Column(nullable = true)
	private String type;
	/**
	 * 文件的本地访问路径
	 */
	@Column(nullable = true)
	private String localPath;
	/**
	 * 上传时原始名称
	 */
	@Column(nullable = true)
	private String originName;
	/**
	 * 上传后存储名称
	 */
	@Column(nullable = true)
	private String saveName;
	/**
	 * 创建时间
	 */
	@Column(nullable = true)
	@Temporal(TemporalType.TIMESTAMP)
//	@JsonFormat(pattern = "yyyy年MM月dd日")
	@JsonFormat(pattern = "yyyy-MM-dd")
	private Date createTime;
	/**
	 * 更新时间
	 */
	@Column(nullable = true)
	@Temporal(TemporalType.TIMESTAMP)
	@JsonFormat(pattern = "yyyy-MM-dd")
	private Date updateTime;
	
	/**
	 * 状态:<br>
	 * <li>TEMP-临时上传，文件关联的内容未保存</li>
	 * <li>SAVED-永久上传，文件关联的内容已保存</li>
	 */
	@Column(nullable = false)
	private String state;
	
    /**
     * 双向多对多</br>
     * 关联关系：t_news~t_news_file~t_file
     */
    @ManyToMany(mappedBy = "files")
//    @JsonBackReference
    private List<News> newss;
	
	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
	
	// ==============
	// constructor
	// ==============
	public File() {
		super();
		// no-args constructor required by JPA spec
		// this one is protected since it shouldn't be used directly
	}

	public File(String id, String type, String localPath, String originName, String saveName,
			Date createTime, Date updateTime, String state, List<News> newss) {
		super();
		this.id = id;
		this.type = type;
		this.localPath = localPath;
		this.originName = originName;
		this.saveName = saveName;
		this.createTime = createTime;
		this.updateTime = updateTime;
		this.state = state;
		this.newss = newss;
	}
	
	@PreUpdate
	public void preUpdate() {
		updateTime = new Date();
	}

	@PrePersist
	public void prePersist() {
		Date now = new Date();
		createTime = now;
		updateTime = now;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getLocalPath() {
		return localPath;
	}

	public void setLocalPath(String localPath) {
		this.localPath = localPath;
	}

	public String getOriginName() {
		return originName;
	}

	public void setOriginName(String originName) {
		this.originName = originName;
	}

	public String getSaveName() {
		return saveName;
	}

	public void setSaveName(String saveName) {
		this.saveName = saveName;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public List<News> getNewss() {
		return newss;
	}

	public void setNewss(List<News> newss) {
		this.newss = newss;
	}

}
